package cn.tedu.sync;

import java.util.concurrent.Semaphore;

public class ShareDemo {
    private static final Semaphore semaphore = new Semaphore(3);

    public static void main(String[] args) throws Exception{
        for (int i = 0; i < 5; i++) {
            new Thread(() -> {
                try {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName() + "获取许可");
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } finally { semaphore.release(); }
            }).start();
        }
    }
}
